package com.ruoyi.system.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mysql.cj.util.TimeUtil;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.SysBusiness;
import com.ruoyi.system.domain.SysBusinessFinance;
import com.ruoyi.system.domain.SysEnterpriseCertification;
import com.ruoyi.system.domain.SysParking;
import com.ruoyi.system.domain.parm.BusinessLoginParm;
import com.ruoyi.system.domain.parm.BusinessPasswordParm;
import com.ruoyi.system.domain.parm.SysBusinessRegParm;
import com.ruoyi.system.service.ISysBusinessFinanceService;
import com.ruoyi.system.service.ISysBusinessService;
import com.ruoyi.system.util.StirngUtil;
import com.ruoyi.system.util.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.TimeoutUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2024-03-24
 */

@Api(tags = "商家用户")
@RestController
@RequestMapping("/bus/business")
public class BusBusinessController extends BaseController
{
    @Autowired
    private ISysBusinessService sysBusinessService;

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ISysBusinessFinanceService sysBusinessFinanceService;
    /**
     * 获取【请填写功能名称】详细信息
     */
    @ApiOperation(value = "商家详情")
    @GetMapping(value = "/info")
    public AjaxResult getInfo(@RequestParam(name = "id",required = true) String id) throws Exception {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new Exception("用户未登陆");
        }

        return success(sysBusinessService.selectSysBusinessById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @ApiOperation(value = "商家注册")
    @PostMapping(value = "/register")
    @Transactional
    public AjaxResult register(@RequestBody @Valid SysBusinessRegParm sysBusinessRegParm) throws Exception {

        SysBusiness sysBusiness = new SysBusiness();
        BeanUtils.copyProperties(sysBusinessRegParm, sysBusiness);
        sysBusiness.setStatus(0);
        String id=UUID.randomUUID().toString();
        sysBusiness.setId(id);
         sysBusinessService.insertSysBusiness(sysBusiness);

        // 添加 商家财务信息
        SysBusinessFinance sysBusinessFinance=new SysBusinessFinance();
        sysBusinessFinance.setBusinessId(id);
        sysBusinessFinance.setAuthentication( 0);
        sysBusinessFinance.setId(UUID.randomUUID().toString());
        sysBusinessFinanceService.insertSysBusinessFinance(sysBusinessFinance);


        return toAjax(1);
    }

    /**
     * 商家登录【请填写功能名称】
     */
    @ApiOperation(value = "商家登录")
    @PostMapping(value = "/login")
    public AjaxResult Login(@RequestBody @Valid BusinessLoginParm businessLoginParm) throws Exception {

        SysBusiness re = sysBusinessService.Login(businessLoginParm);

        if(re.getStatus()==0){
                throw new Exception("用户未激活");
        }
        if(re.getStatus()==2){
            throw new Exception("用户不可用");
        }

    //    String str = StirngUtil.generateRandomString(64);

       String json = JSONArray.toJSONString(re);
     //   redisTemplate.opsForValue().set(str,json, 86400, TimeUnit.SECONDS);

        String token = TokenUtil.create(String.valueOf(re.getId()), re.getUserName(), 36000);
        redisTemplate.opsForValue().set("business_"+re.getUserName(),json, 36000, TimeUnit.SECONDS);

     //   redisTemplate.expire(str, );
        Map result=new HashMap();
        result.put("business",re);
        result.put("Authorization",token);

        return success(result);
    }

    @ApiOperation(value = "获取商家登录信息")
    @GetMapping(value = "/getUserInfo")
    public AjaxResult getUserInfo()
    {
        return success(sysBusinessService.getUserInfo());
    }

    /**
     * 修改【请填写功能名称】
     */
    @ApiOperation(value = "商家修改")
    @PutMapping(value = "/edit")
    public AjaxResult edit(@RequestBody @Valid SysBusinessRegParm sysBusinessRegParm) throws Exception {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new Exception("用户未登陆");
        }
        if(sysBusinessRegParm.getId()==null){
            throw new Exception("id不能为空");
        }
        SysBusiness sysBusiness = new SysBusiness();
        BeanUtils.copyProperties(sysBusinessRegParm, sysBusiness);

        return toAjax(sysBusinessService.updateSysBusiness(sysBusiness));
    }


    /**
     * 修改【请填写功能名称】
     */
    @ApiOperation(value = "商家修改密码")
    @PutMapping(value = "/editPass")
    public AjaxResult editPass(@RequestBody @Valid BusinessPasswordParm businessPasswordParm) throws Exception {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new Exception("用户未登陆");
        }
        if(bu.getId().equals(businessPasswordParm.getId())){
            throw new Exception("用户id错误");
        }

        return toAjax(sysBusinessService.editPass(businessPasswordParm));
    }


}
